﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using baseSet.DAL;
using dhDataAcess.dataAcess;
using dhSystemBLL.basicData;
using baseSet.importExport;

namespace baseSet.datGridSet
{
    public partial class FrmItemListAdd : Form
    {
        itemListBLL item = new itemListBLL();
        moduleListBLL module = new moduleListBLL();
        public FrmItemListAdd()
        {
            InitializeComponent();
        }

        private void FrmItemListAdd_Load(object sender, EventArgs e)
        {
            SqlDataReader reader=readDataTabelColumn.readTableList();
            while (reader.Read())
            {
                litstTableList.Items.Add(reader[0].ToString());
            }
        }

        private void litstTableList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                txtModuldeName.Text = litstTableList.SelectedItem.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {


            sysDataSet.itemListDataTable itemTable = item.getitemListBymoduleNum(txtModuldeName.Text);
            if (itemTable.Rows.Count > 0)
            {
                bSourceItemList.DataSource = itemTable;
                dataGridView1.DataSource = bSourceItemList;
                //return;
            }
            SqlDataReader reader = readDataTabelColumn.readTableByfieldList(litstTableList.Text.ToString());
            int i=0;
            Random ra = new Random();
            while (reader.Read())
            {
                sysDataSet.itemListRow listRow = itemTable.NewitemListRow();

                bool t = false;
                foreach (sysDataSet.itemListRow row in itemTable)
                {
                    if (row.itemName.ToString() == reader[0].ToString())
                    {
                        t = true;
                    }

                }

                if (t) continue;
                listRow.moduleNum = txtModuldeName.Text.ToString();
                listRow.itemNum = txtModuldeName.Text.ToString() + ra .Next (100000000);
                listRow.itemName = reader[0].ToString();
                listRow.itemDispC = txtModuldeName.Text.ToString();
                listRow.itemDispE = txtModuldeName.Text.ToString();
                listRow.itemDispJ = txtModuldeName.Text.ToString();
                listRow.itemWidth = 120;
                listRow.itemDispNo = true;
                listRow.itemDispIndex = itemTable.Rows.Count;
                listRow.itemDataType = reader[1].ToString();
                listRow.createDate = DateTime.Now;
                listRow.createUser = "system";
                listRow.updateDate = DateTime.Now;
                listRow.updateUser = "system";
                listRow.mark = "system";
                i++;
                itemTable.AdditemListRow(listRow);  

            }
            bSourceItemList.DataSource = itemTable;
            dataGridView1.DataSource = bSourceItemList;
            DcolumnSet.colSet(txtModuldeName.Text, dataGridView1 );
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            //检测系统是否有模块名字，如果没有则添加一条记录
           sysDataSet.moduleListDataTable moduleListTable =module.getmoduleListAll (txtModuldeName.Text.ToString());
           if (moduleListTable.Rows.Count == 0)
           {
               dhDataAcess.Model.moduleListModel model=new dhDataAcess.Model.moduleListModel();
               model.moduleName=txtModuldeName.Text.ToString();
               model.moduleNum=txtModuldeName.Text.ToString();
               model.moduleType="基础信息";
               model.createDate=DateTime.Now;
               model.mark="";
               module.moduleListInsert(model);
           }


            //检测表格列信息是否存在，如果存在，先删除该模块表格列的信息再添加
            item.itemListUpdateALL((sysDataSet.itemListDataTable)bSourceItemList.DataSource);
            MessageBox.Show("保存成功");
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
